/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-10-11 16:15:26
 * @version $Id$
 */

;(function(root,factory){

  if(typeof exports === 'object' && typeof module === 'object'){
    
    factory(exports);

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    if (!root._s) {
      root._s = {};
    }
    factory(root);
  }
})(typeof window !== 'undefined' ? window : this ,function(exports, undefined){
  'use strict';

  function moduleInit($, _, utils){

    var pubsub = utils.pubSub()||{};
    var callback = {};
    var callbackFuc = utils.callbackFuc;

    function init(identifier, callbacks){
      
      callback[identifier] = [callbacks];

      pubsub.addEvent(identifier, function(data){
        console.log(callback[identifier]);
        callbackFuc(callback[identifier],data);
      });

      return {
        set: function(identifier, options){
          receiver(identifier, options);
        }
      };
      
    }

    function receiver(identifier, options){

      var result = null;

      switch(identifier){
        case 'history':
        console.log(options);
          result = renderMsg(_, options&&options.data);

        break;
        case 'realMsg':
          result = renderOneMsg(_, options&&options.data);

        break;
        case 'renderedQuick':
          result = renderQuick(_, options&&options.data);

        break;
      }

      pubsub.fireEvent(identifier,result);
      
    };
    /*
      渲染消息
    
    */
    function renderMsg(_, data){

      var msgItemTpl = $('#msgItemTpl');

      $('.chat-container').html(_.template(msgItemTpl.html())({list: data}));
alert(123);
      //$('body')[0].scrollTop = $('.jsService').height()-$(window).height();
      console.log($('.jsService').height());
      console.log($(window).height());

    }
    /*
      渲染单条消息
    */
    function renderOneMsg(_, data){
      console.log(data);
      var msgItemTpl = data[0].type === '2'?$('#imgMsgTpl'):$('#msgItemTpl');

      $('.chat-container').append(_.template(msgItemTpl.html())({list: data}));

      $('body')[0].scrollTop = $('.jsService').height()-$(window).height();

      return {code:0};
    }
    /*
      渲染快捷回复
    */
    function renderQuick(_, data){

      var tpl = $('#replyTpl');

      $('.jsReplyContainer').append(_.template(tpl.html())({list: data}));
      
      $('body')[0].scrollTop = 0;

      return {code:0};
    }

    return {
      receiver: init //接收通知
    };
  }


  exports.renderMsg = moduleInit;

});